#include "TestScene.h"

TestScene::TestScene(LPDIRECT3DDEVICE9 _d3ddev)
{	
	LoadContent(_d3ddev);


	rotationCamera = 0;

	cantX = 20;
	cantY = 20;
	
	CUSTOMVERTEX plane[2400];
	short indicesPlane[2400];

	lightPosition = D3DXVECTOR3(0,4,45);

	int contVertex = 0;
	int contFace =0;
	int contIndex =0;
	
	for(int i=0;i<cantX;i++)
	{
		for(int j=0;j<cantY;j++)
		{
			CUSTOMVERTEX vertex0;
			vertex0.x=i;
			vertex0.y=0;
			vertex0.z=j;
			vertex0.normal = D3DXVECTOR3(0,1,0);
			
			plane[contVertex] = vertex0;
			contVertex++;

			CUSTOMVERTEX vertex1;
			vertex1.x=i+1;
			vertex1.y=0;
			vertex1.z=j;
			vertex1.normal = D3DXVECTOR3(0,1,0);
			plane[contVertex] = vertex1;
			contVertex++;
			
			CUSTOMVERTEX vertex2;
			vertex2.x=i;
			vertex2.y=0;
			vertex2.z=j+1;
			vertex2.normal = D3DXVECTOR3(0,1,0);
			plane[contVertex] = vertex2;
			contVertex++;

			CUSTOMVERTEX vertex3;
			vertex3.x=i+1;
			vertex3.y=0;
			vertex3.z=j+1;
			vertex3.normal = D3DXVECTOR3(0,1,0);
			plane[contVertex] = vertex3;
			contVertex++;

			indicesPlane[contIndex]=contFace;
			contIndex++;
			indicesPlane[contIndex]=contFace+1;
			contIndex++;
			indicesPlane[contIndex]=contFace+2;
			contIndex++;

			indicesPlane[contIndex]=contFace+2;
			contIndex++;
			indicesPlane[contIndex]=contFace+1;
			contIndex++;
			indicesPlane[contIndex]=contFace+3;
			contIndex++;
			
			contFace+=4;
		}
	}
	
	_d3ddev->CreateVertexBuffer(contVertex*sizeof(CUSTOMVERTEX),0,CUSTOMFVF,D3DPOOL_MANAGED,&v_buffer,NULL);
	_d3ddev->CreateIndexBuffer(contIndex*sizeof(short),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&i_buffer,NULL);
	void * pVoid;
	v_buffer->Lock(0,0,(void**)&pVoid,0);	//lockear el buffer
	memcpy(pVoid,plane,sizeof(CUSTOMVERTEX)*contVertex);	//copiar vertices
	v_buffer->Unlock();
	i_buffer->Lock(0,0,(void**)&pVoid,0);
	memcpy(pVoid,indicesPlane,sizeof(short)*contIndex);
	i_buffer->Unlock();

	/*_d3ddev->CreateVertexBuffer(24*sizeof(CUSTOMVERTEX),0,CUSTOMFVF,D3DPOOL_MANAGED,&v_buffer,NULL);
	_d3ddev->CreateIndexBuffer(36*sizeof(short),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&i_buffer,NULL);
	void * pVoid;
	v_buffer->Lock(0,0,(void**)&pVoid,0);	//lockear el buffer
	memcpy(pVoid,OurVertices,sizeof(OurVertices));	//copiar vertices
	v_buffer->Unlock();
	i_buffer->Lock(0,0,(void**)&pVoid,0);
	memcpy(pVoid,indices,sizeof(indices));
	i_buffer->Unlock();*/
}

void TestScene::LoadContent(LPDIRECT3DDEVICE9 _d3ddev)
{	
	_d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE,FALSE);
	_d3ddev->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_ZERO);

	camera = new Camera(D3DXVECTOR3(0.0f,5.0f,-50.0f));
	//camera = new Camera(D3DXVECTOR3(0.0f,0.0f,-50.0f));
	camera->rotate(D3DXVECTOR3(0,0,0));
	
	//Load Meshes////////////////////////////////////////////////////////////////////////////////////////////////////
	//Load Tank
	HRESULT hr = D3DXLoadMeshFromX("flower.x",D3DXMESH_SYSTEMMEM,_d3ddev,NULL,&materialBuffer,NULL,&numMaterials,&mesh);
	
	d3dxTankMaterials = (D3DXMATERIAL *) materialBuffer->GetBufferPointer();

	meshTankMaterials = new D3DMATERIAL9[numMaterials];
	meshTankTextures = new LPDIRECT3DTEXTURE9[numMaterials];
	
	for(DWORD i=0;i<numMaterials;i++)
	{
		//Copiar Material
		meshTankMaterials[i] = d3dxTankMaterials[i].MatD3D;
		//Setea el color ambiente
		meshTankMaterials[i].Ambient = meshTankMaterials[i].Diffuse;
		//crea la textura si existe
		meshTankTextures[i] = NULL;

		if(d3dxTankMaterials[i].pTextureFilename)
			D3DXCreateTextureFromFile(_d3ddev,d3dxTankMaterials[i].pTextureFilename,&meshTankTextures[i]);
	}
	materialBuffer->Release();
	MeshInfo * meshInfoTank = new MeshInfo();
	meshInfoTank->mesh = mesh;
	meshInfoTank->meshMaterials = meshTankMaterials;
	meshInfoTank->meshTextures = meshTankTextures;
	meshInfoTank->numMaterials = numMaterials;

	tank.setMesh(meshInfoTank);
	tank.setPosition(D3DXVECTOR3(0,0,0));
	//tank.setScale(D3DXVECTOR3(1.0f,1.0f,1.0f));
	//tank.setScale(D3DXVECTOR3(0.03f,0.03f,0.03f));
	tank.setScale(D3DXVECTOR3(0.1f,0.1f,0.1f));
	tank.setAngle(D3DXVECTOR3(-10,0,0));

	tank.calculateBoundingBox();
}
TestScene::~TestScene()
{
	v_buffer->Release();
	i_buffer->Release();
	delete camera;
}
void TestScene::Draw(LPDIRECT3DDEVICE9 _d3ddev)
{
	camera->render(_d3ddev);
	tank.Draw(_d3ddev,false);
	//tank.Draw(_d3ddev,v_buffer,i_buffer,cantX*cantY*4,cantX*cantY*2);

}
void TestScene::Update(DWORD dt)
{	
	rotationCamera+=0.01f;
	//tank.setAngle(D3DXVECTOR3(0,rotationTank,0));
	LPDIRECT3DTEXTURE9 text = pRenderTexture;
	camera->setCameraPosition(D3DXVECTOR3(cos(rotationCamera)*70,sin(rotationCamera)*30,sin(rotationCamera)*150));
	camera->rotate(D3DXVECTOR3(0,0,0));
	D3DXVECTOR3 cp = camera->getCameraPosition();
	float* camPos = new float[3];
	camPos[0] = cp.x;
	camPos[1] = cp.y;
	camPos[2] = cp.z;
	cgSetParameter3fv(cameraPos,camPos);
	tank.Update(dt);
	
}
void TestScene::HandleInput(InputManager * _input)
{
	if(_input->isKeyPressed(DIK_UPARROW))
	{
		lightPosition += D3DXVECTOR3(0,0,0.1f);
	}
	if(_input->isKeyPressed(DIK_DOWNARROW))
	{
		lightPosition += D3DXVECTOR3(0,0,-0.1f);
	}
	if(_input->isKeyPressed(DIK_LEFTARROW))
	{
		lightPosition += D3DXVECTOR3(-0.1f,0,0);
	}
	if(_input->isKeyPressed(DIK_RIGHTARROW))
	{
		lightPosition += D3DXVECTOR3(0.1f,0,0);
	}

	if(_input->isKeyPressed(DIK_W))
	{
		tank.setPosition(tank.getPosition()+D3DXVECTOR3(0,0,0.1f));
	}
	if(_input->isKeyPressed(DIK_S))
	{
		tank.setPosition(tank.getPosition()+D3DXVECTOR3(0,0,-0.1f));
	}
	if(_input->isKeyPressed(DIK_A))
	{
		tank.setPosition(tank.getPosition()+D3DXVECTOR3(-0.1f,0,0));
	}
	if(_input->isKeyPressed(DIK_D))
	{
		tank.setPosition(tank.getPosition()+D3DXVECTOR3(0.1f,0,0));
	}
}
void TestScene::Init()
{

}